﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace MessageSystem.Managed.Infrastructure
{
    public class BufferManager
    {
        public unsafe class Buffer
        {
            public NetStream Stream { get; private set; }
            public byte[] Data { get; private set; }
            public byte* pData { get; private set; }
            public int Size { get; private set; }

            public void Create(int size)
            {
                Stream = new NetStream();
                Size = size;
                Data = new byte[Size];
                GCHandle h = GCHandle.Alloc(Data, GCHandleType.Pinned);
                unsafe
                {
                    pData = (byte*)h.AddrOfPinnedObject().ToPointer();
                    Stream.Reset(pData, (UInt32)Data.Length);
                }
            }
        }

        public Buffer OutputSteam { get; private set; }
        public Buffer ZipStream { get; private set; }

        public static BufferManager Instance = new BufferManager();

        public BufferManager()
        {
            OutputSteam = new Buffer();
            OutputSteam.Create(UInt16.MaxValue);

            ZipStream = new Buffer();
            ZipStream.Create(UInt16.MaxValue);
        }
    }
}
